#Rose online ZMS parser

import os, sys
from Sanae3D.lib.ModelObject import Model3D
from Sanae3D.lib.StructReader import StructReader

class RoseOnline_zms(Model3D):
    
    def __init__(self, obj3D=None, outFile=None, inFile=None):
        
        super(RoseOnline_zms,self).__init__("XAC")
        self.obj3D = obj3D
        self.inFile = inFile
        self.outFile = outFile
        self.zmsType = 0
        
    def parse_bones(self):
        
        numBones = self.inFile.read_short(1)
        boneIDs = self.inFile.read_short(numBones)
        print boneIDs
        
    def parse_strips(self):
        
        numStrips = self.inFile.read_short()
        strips = self.inFile.read_short(numStrips)
        
    def parse_material(self, filename):
        
        self.create_material(0)
        self.add_material_name(0, filename)
        self.add_texture_name(0, filename + ".dds")
        
    def parse_materials(self):
        
        numMat = self.inFile.read_short()
        matIDs = self.inFile.read_short(numMat)
        
    def parse_faces(self, meshName):
        
        numFaces = self.inFile.read_short()
        faces = self.inFile.read_short(3*numFaces)
        for i in range(numFaces):
            offset = i * 3
            v1 = faces[offset]
            v2 = faces[offset+1]
            v3 = faces[offset+2]
            
            self.create_face(meshName, i)
            self.add_face_verts(meshName, i, [v1, v2, v3])
            self.add_face_material(meshName, i, 0)
            
    def parse_vertices(self, meshName, numVerts):
        
        coords = self.inFile.read_float(3*numVerts)
        for i in xrange(numVerts):
            offset = i*3
            vx = coords[offset]
            vy = coords[offset+1]
            vz = coords[offset+2]
            
            vx = float(vx) * -1
            self.create_vertex(meshName, i)
            self.add_coords(meshName, i, [vx, vy, vz])
        
    def parse_normals(self, meshName, numVerts):
        
        normals = self.inFile.read_float(numVerts*3)
        for i in xrange(numVerts):
            offset = i*3
            nx = normals[offset]
            ny = normals[offset+1]
            nz = normals[offset+2]
            self.add_vert_normals(meshName, i, [nx, ny, nz])
            
    def parse_weights(self, meshName, numVerts):
        
        for i in xrange(numVerts):
            w1, w2, w3, w4 = self.inFile.read_float(4)
            b1, b2, b3, b4 = self.inFile.read_short(4)
            
    def parse_uv(self, meshName, numVerts):
                
        uvs = self.inFile.read_float(2*numVerts)
        for i in xrange(numVerts):
            offset = i*2
            tu = uvs[offset]
            tv = uvs[offset+1]
            
            self.add_vert_uv(meshName, i, [tu, tv])
            
    def parse_uv2(self, meshName, numVerts):
        
        uv2 = self.inFile.read_float(2*numVerts)
        
    def parse_file(self, filename):
        '''Main parser method. Can be replaced'''
                
        magic = self.inFile.read_string(8)
        zmsType = self.inFile.read_long()
        bbMin = self.inFile.read_float(3)
        bbMax = self.inFile.read_float(3)
        
        self.parse_bones()
        
        meshName = filename
        self.create_object(meshName)
        
        numVerts = self.inFile.read_short()
        self.parse_vertices(meshName, numVerts)
        self.parse_normals(meshName, numVerts)
        if zmsType == 182:
            self.parse_weights(meshName, numVerts)
        self.parse_uv(meshName, numVerts)
        if zmsType == 390:
            self.parse_uv2(meshName, numVerts)
            
        self.parse_faces(meshName)
        self.parse_materials()
        
def read_file(path):
    '''Read the file'''
    
    filename, ext = os.path.splitext(os.path.basename(path))
    openFile = StructReader(open(path, 'rb'))
    obj = RoseOnline_zms(inFile=openFile)
    obj.parse_material(filename)
    obj.parse_file(filename)
    openFile.close()
    return obj

def write_file(path):
    
    pass

def definitions():
    '''Return the header, extension, and a description of the format'''
    
    return "ZMS000", "ZMS", "Rose Online ZMS"

if __name__ == '__main__':
    
    file1 = "BOG6.ZMS"
    file2 = "DI5.ZMS"
    file3 = "CB_XBLADE.ZMS"
    file4 = "CATACOMBS01.ZMS"
    obj = read_file(file4)